package com.itheima.entry;

//二分法排序
// 数据元素是有序的
public class TestBinarySearchByIndex {

    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6,7,8,9,10};
        int s=-2;
          int l=BinarySearch(arr,s);
        System.out.println(l);

    }
    public static int BinarySearch(int[] arr,int s){
        int min=0;
        int max=arr.length-1;
            while(min<=max) {
               int mid=(min+max)/2;

                if (arr[mid]<s){
                    min=mid+1;


                }else if(arr[mid]>s){
                    max=mid-1;

                }else return mid;
            }
          return -1;
        }

}
